import { BasicColumn, FormSchema } from '@/components/Table';
import { getTeacherListApi } from './service';
import dayjs from 'dayjs';
import { getStudyRoomListApi } from '../studyRoom/service';

export const getTableColumns = (): BasicColumn[] => [
  {
    title: '日期',
    dataIndex: 'Date',
    width: 120,
    customRender: ({ text }: any) => {
      return dayjs(text).format('YYYY-MM-DD');
    },
  },
  {
    title: '值班时段',
    dataIndex: 'ShiftType',
    width: 150,
    customRender: ({ text }: any) => {
      return text === 'Morning' ? '上午' : text === 'Afternoon' ? '下午' : '晚班';
    },
  },
  {
    title: '值班自习室名称',
    dataIndex: 'RoomName',
    width: 150,
  },
  {
    title: '值班教师',
    dataIndex: 'TeacherName',
    width: 150,
  },
];

export function getTableFormSchema(): FormSchema[] {
  return [
    {
      field: 'RoomID',
      label: '自习室名称',
      component: 'ApiSelect',
      componentProps: {
        api: async () => {
          const res = await getStudyRoomListApi({ page: 1, pageSize: 100 });
          return res.data;
        },
        resultField: 'records',
        labelField: 'RoomName',
        valueField: 'RoomID',
      },
    },
    {
      field: 'ShiftType',
      label: '班次',
      component: 'Select',
      componentProps: {
        options: [
          {
            label: '上午',
            value: 'Morning',
          },
          {
            label: '下午',
            value: 'Afternoon',
          },
          {
            label: '晚班',
            value: 'Evening',
          },
        ],
      },
    },
    {
      field: 'TeacherID',
      label: '教师姓名',
      component: 'ApiSelect',
      componentProps: {
        api: getTeacherListApi,
        resultField: 'records',
        labelField: 'Name',
        valueField: 'TeacherID',
      },
    },
  ];
}

export const getActionModalSchemas = (): FormSchema[] => {
  return [
    {
      field: 'DutyScheduleID',
      label: 'DutyScheduleID',
      show: false,
      component: 'Input',
    },
    {
      field: 'Date',
      label: '值班日期',
      component: 'DatePicker',
      required: true,
      componentProps: {
        style: {
          width: '100%',
        },
        format: 'YYYY-MM-DD',
        disabledDate: (current: any) => {
          return current && current < new Date().setHours(0, 0, 0, 0);
        },
      },
    },
    {
      field: 'RoomID',
      label: '自习室',
      component: 'ApiSelect',
      required: true,
      componentProps: ({ formModel }) => ({
        api: async () => {
          const res = await getStudyRoomListApi({ page: 1, pageSize: 100 });
          formModel.studyRoomOptions = res.data.records;
          return res.data;
        },
        resultField: 'records',
        labelField: 'RoomName',
        valueField: 'RoomID',
        onChange: (RoomID) => {
          // 从暂存的选项中查找对应RoomName
          const target = (formModel.studyRoomOptions || []).find((item) => item.RoomID === RoomID);
          // 设置RoomName字段值
          formModel.RoomName = target?.RoomName || '';
        },
      }),
    },
    {
      field: 'RoomName',
      label: '自习室名称',
      component: 'Input',
      required: true,
      show: false,
    },
    {
      field: 'TeacherID',
      label: '值班教师',
      component: 'ApiSelect',
      required: true,
      componentProps: {
        api: getTeacherListApi,
        resultField: 'records',
        labelField: 'Name',
        valueField: 'TeacherID',
      },
    },
    {
      field: 'ShiftType',
      label: '值班时段',
      component: 'Select',
      required: true,
      componentProps: {
        options: [
          {
            label: '上午',
            value: 'Morning',
          },
          {
            label: '下午',
            value: 'Afternoon',
          },
          {
            label: '晚班',
            value: 'Evening',
          },
        ],
      },
    },
  ];
};
